package 牛牛冲钻石;

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T-- > 0) {
            int n = in.nextInt();
            int k = in.nextInt();
            String s = in.next();
            int w = 0, res = 0;
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == 'W') {
                    w++;
                    if (w >= 3) {
                        res += k;
                    } else {
                        res++;
                    }
                } else {
                    w = 0;
                    res--;
                }
            }
            System.out.println(res);
        }
    }
}